// pages/new_born_zone/index.js
import {
    getNavigationbar,
} from "../../api/index.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navigateName: '新人专享',
        newcomerList: [],
        nerList: [],currentPage:1,
        active:'',
        acId: '',isSpications:false,ipecs:false,
    },
    close() {
        this.setData({
            ipecs: false,
            isSpications:false,
           
            btnText: "确定",
            isShowt:false,
            confirmText: "toOrder"
        })
    },
    seckillFn(e){
        const index = e.currentTarget.dataset.index
        this.setData({
            active: index
        },()=>{
            this.data.currentPage = 1
            this.data.newcomerList = []
            this.getUnconsumeRegion(index)
        })
    },
    scrollToLower() {
        this.setData({
            currentPage: this.data.currentPage + 1,
           
        });
        this.getUnconsumeRegion(this.data.active)
    },
    // 检测
    gethasShopping() {
        getNavigationbar({
            act: 'has_shopping',
        }).then(res => {
            console.log('检测', res)
            if (res.has_shopping == 1) {
                wx.showModal({
                    title: '提示',
                    content: '店铺已下单,不可享受新人专区优惠',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            wx.navigateBack()
                        }
                    }
                })
            }
        })
    },
    // 新人专享
    getUnconsumeRegion(index) {
        getNavigationbar({
            act: 'unconsume_region',
        }).then(res => {
            console.log('新人专项', res)
            this.setData({
                nerList: res,
            })
            if (res.length > 0) {
                this.setData({
                    // newcomerList: res,
                    acId: res[index].id,
                }, () => {
                    getNavigationbar({
                        act: 'unconsume_region_list',
                        id: this.data.acId,
                        p: this.data.currentPage, // 当前页码
                        lack_inventory: '1',
                    }).then(res => {
                        console.log('新人专项2', res)
                        // this.newcomerList =
                        this.setData({
                         
                            newcomerList: this.data.newcomerList.concat(res) 
                        })
                    })
                })
            }
        })
    },
    /**
     * 跳转商品详情页回调函数方法
     */
    handleGoodsDetail(e) {
        const {
            value
        } = e.currentTarget.dataset
        console.log('e', value)
        const params = {
            goodsId: value.id,
            detailType: '7',
            act: 'unconsume_region_detail',
        }
        wx.navigateTo({
            url: `/pages/goods_detail/index?data=${JSON.stringify(params)}`,
        })
    },
    /**
     * 加入购物车
     */
    handleJoinCart(e) {
        const {
            value
        } = e.currentTarget.dataset
        console.log("value", value)
        
        if (this.data.obj.shop_status == 0 || this.data.obj.shop_status == 1) {
            this.setData({
                isAhow: true,
            })
            return
        }
        let data = {
            // goodsid: val.goodsid,
            // has_inventorys: val.has_inventorys,
            // id: val.id,
            // marketprice: val.marketprice,
            // price: val.price,
            // property_price: val.property_price,
            // propertyid: val.propertyid,
            // thumb: val.thumb,
            // type: 'unconsume_region_detail',
            ...value
        }
        // let data = {
        //     goodsid:value.goodsid,
           
        //     has_inventorys: value.has_inventorys,
        //     id: value.id,
        //     marketprice: value.marketprice,
        //     price: value.price,
        //     property_price: value.property_price,
        //     propertyid: value.propertyid,
        //     thumb: value.thumb,
        //     type:'unconsume_region_detail'
        // }
        // wx.navigateTo({
        //     url: `/subpackages/pages/confirm_order_sh/index?data=` + JSON.stringify(data)
        // })
        // getNavigationbar({
        //     act: 'unconsume_region_detail',
        //     id: value.id
        // }).then(res => {
        //     console.log(res)
        //     res.detailType = 7
        //     if (res.type == null) {
        //         res.type = 0
        //     }
        //     this.setData({
        //         isSpecs: true,
        //         cartGoodDetail: res
        //     })
        //     console.log(this.data.cartGoodDetail)
        // })
        this.setData({
            ipecs: true,
            isSpications:true,
            cartGoodDetail: [data],
            btnText: "确定",
            isShowt:false,
            confirmText: "toOrder"
        })

    },
    confirml(e) {
        console.log('e',e)
        let val = e.detail
        let data = {
            goodsid: val.goodsid,
            has_inventorys: val.has_inventorys,
            id: val.id,
            marketprice: val.marketprice,
            price: val.price,
            property_price: val.property_price,
            propertyid: val.propertyid,
            thumb: val.thumb,
            type: 'unconsume_region_detail',
            totalNumbers:val.count || 1
        }
        console.log('提交', data)
        wx.navigateTo({
            url: `/subpackages/pages/confirm_order_sh/index?data=` + JSON.stringify(data) 
        })
        // setInterval(()=>{
        //     this.setData({
        //         ipecs: false,
        //         isSpications:false,
             
        //         btnText: "确定",
        //         isShowt:false,
        //         confirmText: "toOrder"
        //     })
        // },1000)
    },
    /**
     * 完成规格选择将商品添加至购物车
     */
    handleConfirm(e) {
        let data = e.detail
        console.log('传回', data)
        this.setData({
            isSpecs: false
        })

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.gethasShopping()
        this.getUnconsumeRegion(0);
        let res = wx.getStorageSync('USER')
        this.data.obj = res
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})